﻿using System;
using System.Collections.Generic;
using System.Data;
using SGIPSPManager.Domain;
using Spring.Data.Generic;
using Framework.Core.Domain;
using Spring.Data.Common;
using Spring.Dao;

namespace SGIPSPManager.DAOImpl
{
    public class MTMessageMSSQLImpl : AdoDaoSupport, IMTMessageDAO
    {
        #region 常量
        private const string SQL_SELECT = "Select * From T_{0}MT";
        private const string SQL_SELECT_ONE = "Select * From T_{0}MT Where ID = @ID";
        private const string SQL_INSERT = @"Insert Into T_{0}MT(ID, SubmitDate, ScrID, ChargeNumber, UserNumber, SPID, ServiceType, FeeType, FeeValue, GivenValue, AgentFlag, MorelatetoMTFlag, Priority, ExpireTime, ScheduleTime, ReportFlag, TP_pid, TP_udhi, MessageCoding, MessageType, MessageLength, MessageContent, Status, ReSendTimes) 
Values(@ID, @SubmitDate, @ScrID, @ChargeNumber, @UserNumber, @SPID, @ServiceType, @FeeType, @FeeValue, @GivenValue, @AgentFlag, @MorelatetoMTFlag, @Priority, @ExpireTime, @ScheduleTime, @ReportFlag, @TP_pid, @TP_udhi, @MessageCoding, @MessageType, @MessageLength, @MessageContent, @Status, @ReSendTimes)
Set @SeqID = IDENT_CURRENT('T_{0}MT') Select @SeqID";

        private const string SQL_DELETE = "Delete From T_{0}MT Where 1=1 ";
        private const string SQL_UPDATE = @"Update T_{0}MT Set 
SubmitDate=@SubmitDate, 
ChargeNumber=@ChargeNumber, 
UserNumber=@UserNumber, 
ServiceType=@ServiceType, 
FeeType=@FeeType, 
FeeValue=@FeeValue, 
GivenValue=@GivenValue, 
AgentFlag=@AgentFlag, 
MorelatetoMTFlag=@MorelatetoMTFlag, 
Priority=@Priority, 
ExpireTime=@ExpireTime, 
ScheduleTime=@ScheduleTime, 
ReportFlag=@ReportFlag, 
TP_pid=@TP_pid, 
TP_udhi=@TP_udhi, 
MessageCoding=@MessageCoding, 
MessageType=@MessageType, 
MessageLength=@MessageLength, 
MessageContent=@MessageContent, 
Status=@Status, 
ReSendTimes=@ReSendTimes Where 1=1 ";
        #endregion

        #region IDaoBase<MTMessage,long> Members

        public void Delete(MTMessage entity)
        {
            AdoTemplate.ExecuteNonQuery(CommandType.Text, string.Format(SQL_DELETE, entity.ScrID) + "And ID = @ID", "ID", DbType.Guid, 40, entity.ID);
        }

        public MTMessage GetById(Guid entityId, string scrID)
        {
            try
            {
                return AdoTemplate.QueryForObject<MTMessage>(CommandType.Text, string.Format(SQL_SELECT_ONE, scrID), new MTMessageRowMapper(), "ID", DbType.Guid, 40, entityId);
            }
            catch (EmptyResultDataAccessException)
            {
                return null;
            }
        }

        public void Save(MTMessage entity)
        {
            Guid id = entity.ID == Guid.Empty ? Guid.NewGuid() : entity.ID;
            IDbParameters dbParams = AdoTemplate.CreateDbParameters();
            dbParams.Add("ID", DbType.Guid).Value = id;
            dbParams.Add("AgentFlag", DbType.Byte).Value = (byte)entity.AgentFlag;
            dbParams.Add("ChargeNumber", DbType.String).Value = entity.ChargeNumber;
            dbParams.Add("SPID", DbType.String).Value = entity.SPID;
            dbParams.Add("ExpireTime", DbType.DateTime).Value = entity.ExpireTime;
            dbParams.Add("FeeType", DbType.Byte).Value = (byte)entity.FeeType;
            dbParams.Add("FeeValue", DbType.String).Value = entity.FeeValue;
            dbParams.Add("GivenValue", DbType.String).Value = entity.GivenValue;
            dbParams.Add("MessageCoding", DbType.Byte).Value = (byte)entity.MessageCoding;
            dbParams.Add("MessageContent", DbType.String).Value = entity.MessageContent;
            dbParams.Add("MessageLength", DbType.Int32).Value = entity.MessageLength;
            dbParams.Add("MessageType", DbType.Byte).Value = (byte)entity.MessageType;
            dbParams.Add("MorelatetoMTFlag", DbType.Byte).Value = (byte)entity.MorelatetoMTFlag;
            dbParams.Add("Priority", DbType.Int16).Value = entity.Priority;
            dbParams.Add("ReportFlag", DbType.Byte).Value = (byte)entity.ReportFlag;
            dbParams.Add("ScheduleTime", DbType.DateTime).Value = entity.ScheduleTime;
            dbParams.Add("ServiceType", DbType.String).Value = entity.ServiceType;
            dbParams.Add("ScrID", DbType.String).Value = entity.ScrID;
            dbParams.Add("SubmitDate", DbType.DateTime).Value = entity.SubmitDate;
            dbParams.Add("TP_pid", DbType.Int32).Value = entity.TP_pid;
            dbParams.Add("TP_udhi", DbType.Int32).Value = entity.TP_udhi;
            dbParams.Add("UserNumber", DbType.String).Value = entity.UserNumber;
            dbParams.Add("Status", DbType.Byte).Value = (byte)entity.Status;
            dbParams.Add("ReSendTimes", DbType.Int32).Value = entity.ReSendTimes;
            dbParams.AddOut("SeqID", DbType.Int64);
            AdoTemplate.ExecuteNonQuery(CommandType.Text, string.Format(SQL_INSERT, entity.ScrID), dbParams);
            entity.SeqID = long.Parse(dbParams["@SeqID"].Value.ToString());
            entity.ID = id;
        }

        public void SaveOrUpdate(MTMessage entity)
        {
            if (this.GetById(entity.ID, entity.ScrID) == null)
            {
                this.Save(entity);
            }
            else
            {
                this.Update(entity);
            }
        }

        public void Update(MTMessage entity)
        {
            IDbParameters dbParams = AdoTemplate.CreateDbParameters();
            dbParams.Add("ID", DbType.Guid).Value = entity.ID;
            dbParams.Add("AgentFlag", DbType.Byte).Value = (byte)entity.AgentFlag;
            dbParams.Add("ChargeNumber", DbType.String).Value = entity.ChargeNumber;
            dbParams.Add("ExpireTime", DbType.DateTime).Value = entity.ExpireTime;
            dbParams.Add("FeeType", DbType.Byte).Value = (byte)entity.FeeType;
            dbParams.Add("FeeValue", DbType.String).Value = entity.FeeValue;
            dbParams.Add("GivenValue", DbType.String).Value = entity.GivenValue;
            dbParams.Add("MessageCoding", DbType.Byte).Value = (byte)entity.MessageCoding;
            dbParams.Add("MessageContent", DbType.String).Value = entity.MessageContent;
            dbParams.Add("MessageLength", DbType.Int32).Value = entity.MessageLength;
            dbParams.Add("MessageType", DbType.Byte).Value = (byte)entity.MessageType;
            dbParams.Add("MorelatetoMTFlag", DbType.Byte).Value = (byte)entity.MorelatetoMTFlag;
            dbParams.Add("Priority", DbType.Int16).Value = entity.Priority;
            dbParams.Add("ReportFlag", DbType.Byte).Value = (byte)entity.ReportFlag;
            dbParams.Add("ScheduleTime", DbType.DateTime).Value = entity.ScheduleTime;
            dbParams.Add("ServiceType", DbType.String).Value = entity.ServiceType;
            dbParams.Add("SubmitDate", DbType.DateTime).Value = entity.SubmitDate;
            dbParams.Add("TP_pid", DbType.Int32).Value = entity.TP_pid;
            dbParams.Add("TP_udhi", DbType.Int32).Value = entity.TP_udhi;
            dbParams.Add("UserNumber", DbType.String).Value = entity.UserNumber;
            dbParams.Add("Status", DbType.Byte).Value = (byte)entity.Status;
            dbParams.Add("ReSendTimes", DbType.Int32).Value = entity.ReSendTimes;
            AdoTemplate.ExecuteNonQuery(CommandType.Text, string.Format(SQL_UPDATE, entity.ScrID) + "And ID = @ID", dbParams);
        }

        #endregion
    }
}
